import {
	observable,
	action,
	computed,
	autorun,
} from 'mobx';
import {
	WeaCheckbox,
	WeaLocaleProvider,
} from 'ecCom';
import {
	message,
} from 'antd';
import {
	fetch,
	componentReset,
} from '../util/save-time-util.js';
import {
	commonStore
} from './commonStore';
import * as asyncFetch from '../apis/originalPunchRecord';
const getLabel = WeaLocaleProvider.getLabel;

class HrmOriginalPunchRecord {
	pageConfig = {
		asyncFetch: asyncFetch,
		hasAdvancedSearch: false,
		hasRadioGroup: true,
		hasOrgTree: false,
		permissAsyncName: 'getHasRight',
		menusAsyncName: 'getMenus',
		radioGroupAsyncName: 'getSearchCondition',
		tableAsyncName: 'getSearchList',
	}

	//*************头部**************
	@observable isNoAccountShow = false;

	@action clickIcon = (bool) => {
		this.isNoAccountShow = bool;

		commonStore.getSearchList();
	}

	excel = () => {
		commonStore.table.tableStore.exportAll();
	}

	custom = () => {
		const {
			table
		} = commonStore, {
			tableStore
		} = table;

		['setColSetVisible', 'tableColSet'].map(f => {
			tableStore[f](true);
		});

	}

	refresh = () => {
		commonStore.getSearchList();
	}

	//获取交互元素（按钮元素和下拉菜单元素）的状态
	@computed get menuStatus() {
		const {
			tableStore
		} = commonStore.table;

		return [tableStore.loading];
	}

	//***************单选组合（RadioGroup）*****************
	@observable checkedItems = '';

	addCustomizedConfig = (conditions, callback) => {
		const {
			option,
			value,
		} = callback;

		this.checkedItems = value;

		const com = <div>{
			option.map((opt, i) => {
				if (Array.isArray(opt)) {
					return (
						<div>{opt.map((obj, index) => {
							const {
								key,
								showname
							} = obj;
							return (
								<WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@mdapla@${index}`} id={key} content={showname} value={this.checkedItems.split(',').includes(key)} onChange={val => this.collectCheckedItem(val,key)}/>
							)
						})}</div>
					)
				} else {
					const {
						key,
						showname
					} = opt;

					return (
						<WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@ds1r8p@${i}`} id={key} content={showname} value={this.checkedItems.split(',').includes(key)} onChange={val => this.collectCheckedItem(val,key)}/>
					)
				}
			})
		}</div>;

		conditions[0].items.unshift({
			com,
			label: getLabel('390081', "展示列"),
			labelcol: 4,
			fieldcol: 20,
		})
	}

	@action collectCheckedItem = (val, key) => {
		const keys = this.checkedItems ? this.checkedItems.split(',') : [];

		const add = (key) => {
			keys.push(key);
		};

		const del = (key) => {
			const targetIndex = keys.findIndex(_key => _key === key);
			keys.splice(targetIndex, 1);
		};

		(val === '1') ? add(key): del(key);

		this.checkedItems = keys.toString();

		commonStore.getSearchList();
	}

	@computed get radioGroupCustomizedComParams() {
		return {
			showCol: this.checkedItems,
			isNoAccount: this.isNoAccountShow,
		};
	}

	isRequiredElementComplete = (params) => {
		const {
			dateScope,
			dataScope,
			startDate,
			endDate,
			subcomId,
			deptId,
			resourceId,
		} = params;

		if (dateScope === '6' && (!startDate || !endDate)) return false;

		if (dataScope === '1' && !subcomId) return false;

		if (dataScope === '2' && !deptId) return false;

		if (dataScope === '3' && !resourceId) return false;

		return true;
	}

	refreshPage = () => {
		this.isNoAccountShow = false;

	}


}

const hrmOriginalPunchRecord = new HrmOriginalPunchRecord();

export {
	hrmOriginalPunchRecord
}